#include "OrbitSystem.h"


OrbitSystem::OrbitSystem( ) : 
	m_pBatchNode(0) , 
	m_fRadius(0) , 
	m_fNextRadius(0),
	m_fAncientRadius(0),
	m_iNumSegment(0) , 
	m_pListActiveCircle(0)
{

}


OrbitSystem::~OrbitSystem( )
{
	if( m_pListActiveCircle ){
		m_pListActiveCircle->removeAllObjects();
		m_pListActiveCircle->release();
	}
}


void OrbitSystem::initWithRadius( float radius , int segment )
{
	m_fRadius = radius;
	m_iNumSegment = segment;

	m_pListActiveCircle = new CCArray();

	//if( m_pBatchNode != NULL ){
	//float r = 15;
		float dr = 360.0f / segment;
		float curR = 0;
		
		for( int i = 0 ; i < segment ; ++i )
		{
			CCSprite* pSprite = LTextureHelper::createSprite( &s_pPathResOrbit );
			pSprite->setPosition( ccp( m_fRadius * sinf( CC_DEGREES_TO_RADIANS( curR ) ) , m_fRadius * cosf( CC_DEGREES_TO_RADIANS( curR )  ) ) );
			//m_pBatchNode->addChild( pSprite );
			pSprite->setTag( ORBIT_CHILD_ORBIT_SEGMENT );
			this->addChild( pSprite );
			curR += dr;
		}
	//}

}



void OrbitSystem::addCircle( Circle* pCircle )
{
	if( m_pListActiveCircle ){
		m_pListActiveCircle->addObject( pCircle );

		pCircle->setRadius( m_fRadius );
		//pCircle->setPosition( ccp( m_fRadius * sinf( CC_DEGREES_TO_RADIANS( startAngle ) ) , m_fRadius * cosf( CC_DEGREES_TO_RADIANS( startAngle ) ) ) );
		pCircle->setTag( ORBIT_CHILD_ORBIT_CIRCLE );
		this->addChild( pCircle );
	}
}


void OrbitSystem::removeCircle( Circle* pCircle )
{
	if( m_pListActiveCircle ){
		m_pListActiveCircle->removeObject(pCircle);

		if( m_pListActiveCircle->count() == 0 ){
			// LOC: TODO : Kill it
			runFadeOutAnimation( );
			
			NotificationData* pData = NotificationData::create();
			pData->setObject( NOTIOBJECT_NAME_ORBIT , this );

			NotificationCenter::sharedNotificationCenter()->post( pData , NOTI_ON_REMOVE_ORBIT );
		}
	}
}


void OrbitSystem::runFadeOutAnimation( )
{
	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pChildren , pObj )
	{
		CCNode* pChild = (CCNode*)pObj;
		pChild->runAction( CCFadeOut::create(1) );
	}
}


void OrbitSystem::runFadeInAnimation( )
{
	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pChildren , pObj )
	{
		CCNode* pChild = (CCNode*)pObj;
		pChild->runAction( CCFadeIn::create(1) );
	}
}

void OrbitSystem::runToOriginAnimation( float nextRadius )
{
	m_fAncientRadius = m_fRadius;
	m_fNextRadius = nextRadius;

	if( !this->isVisible( ) )
	{
		this->runFadeInAnimation();
	}

	this->schedule( schedule_selector( OrbitSystem::animationUpdate ) );
}


void OrbitSystem::animationUpdate( float dt )
{
	setRadius( m_fRadius - ( m_fAncientRadius - m_fNextRadius ) * dt );

	if( m_fRadius <= m_fNextRadius ){
		setRadius(m_fNextRadius);
		this->unschedule( schedule_selector( OrbitSystem::animationUpdate ) );
	}
}

void OrbitSystem::setRadius( float radius )
{
	float oldRadius = m_fRadius;
	m_fRadius = radius;

	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pChildren , pObj )
	{
		CCNode* pChild = (CCNode*)pObj;
		if( pChild->getTag() == ORBIT_CHILD_ORBIT_SEGMENT ){
			if( oldRadius != 0 ){
				float sinAngleX = pChild->getPositionX() / oldRadius;
				float cosAngleY = pChild->getPositionY() / oldRadius;
				pChild->setPosition( ccp( m_fRadius * sinAngleX , m_fRadius * cosAngleY ) );
			}else{
				pChild->setPosition( CCPointZero );
			}
		}else if( pChild->getTag() == ORBIT_CHILD_ORBIT_CIRCLE ){
			((Circle*)pChild)->setRadius( radius );
		}
	}
}